<template>
	<view class="container">
		<view class="header">
			<view class="title">推荐排行</view>
			<view class="tabs">
				<view 
					class="tab-item" 
					:class="{ active: currentTab === 1 }"
					@click="switchTab(1)"
				>员工排行</view>
				<view 
					class="tab-item" 
					:class="{ active: currentTab === 2 }"
					@click="switchTab(2)"
				>非员工排行</view>
			</view>
		</view>
		
		<view class="content">
			<scroll-view scroll-y="true" @scrolltolower="loadData" class="scroll-view">
				<!-- 员工排行 -->
				<view v-if="currentTab === 1" class="rank-list">
					<scroll-view scroll-x="true" class="table-scroll">
						<view class="table-content">
							<view class="rank-header">
								<view class="rank-col rank">排名</view>
								<view class="rank-col user-info">用户信息</view>
								<view class="rank-col count">浏览量</view>
								<view class="rank-col count">分享量</view>
								<view class="rank-col count">直推数</view>
								<view class="rank-col amount">直推金额</view>
								<view class="rank-col amount">溯源金额</view>
								<view class="rank-col amount">奖励红包</view>
							</view>
							<view class="rank-item" v-for="(item, index) in staffRankList" :key="item.user_id">
								<view class="rank-col rank">{{index + 1}}</view>
								<view class="rank-col user-info">
									<image :src="item.avatar_url" class="avatar"></image>
									<text class="nickname">{{item.nickname}}</text>
								</view>
								<view class="rank-col count">{{item.visit_count}}</view>
								<view class="rank-col count">{{item.share_count}}</view>
								<view class="rank-col count">{{item.recommend_count}}</view>
								<view class="rank-col amount">¥{{item.direct_amount}}</view>
								<view class="rank-col amount">¥{{item.trace_amount}}</view>
								<view class="rank-col amount">¥{{item.reward_amount}}</view>
							</view>
						</view>
					</scroll-view>
				</view>
				
				<!-- 非员工排行 -->
				<view v-if="currentTab === 2" class="rank-list">
					<scroll-view scroll-x="true" class="table-scroll">
						<view class="table-content">
							<view class="rank-header">
								<view class="rank-col rank">排名</view>
								<view class="rank-col user-info">用户信息</view>
								<view class="rank-col count">浏览量</view>
								<view class="rank-col count">分享量</view>
								<view class="rank-col count">直推数</view>
								<view class="rank-col amount">直推金额</view>
								<view class="rank-col amount">奖励红包</view>
							</view>
							<view class="rank-item" v-for="(item, index) in nonStaffRankList" :key="item.user_id">
								<view class="rank-col rank">{{index + 1}}</view>
								<view class="rank-col user-info">
									<image :src="item.avatar_url" class="avatar"></image>
									<text class="nickname">{{item.nickname}}</text>
								</view>
								<view class="rank-col count">{{item.visit_count}}</view>
								<view class="rank-col count">{{item.share_count}}</view>
								<view class="rank-col count">{{item.recommend_count}}</view>
								<view class="rank-col amount">¥{{item.direct_amount}}</view>
								<view class="rank-col amount">¥{{item.reward_amount}}</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import * as api from '@/api/staff'
	import storage from '@/utils/storage'
	
	export default {
		data() {
			return {
				currentTab: 1, // 默认显示员工排行
				activityId: 0,
				staffRankList: [],
				nonStaffRankList: [],
				page: 1,
				page_size: 20,
				hasMore: true
			}
		},
		onLoad() {
			this.activityId = storage.get('activityId')
			this.loadData()
		},
		methods: {
			switchTab(index) {
				this.currentTab = index
				this.page = 1
				this.hasMore = true
				if (index === 1) {
					this.staffRankList = []
				} else {
					this.nonStaffRankList = []
				}
				this.loadData()
			},
			loadData() {
				if (!this.hasMore) return
				
				api.getRankList({
					activity_id: this.activityId,
					type: this.currentTab, // 1: 员工, 2: 非员工
					page: this.page,
					limit: this.page_size
				}).then(res => {
					if (res.code === 200) {
						const list = res.data || []
						if (this.currentTab === 1) {
							this.staffRankList = [...this.staffRankList, ...list]
						} else {
							this.nonStaffRankList = [...this.nonStaffRankList, ...list]
						}
						this.hasMore = list.length > 0
						this.page++
					} else {
						uni.showToast({
							title: res.msg || '获取数据失败',
							icon: 'none'
						})
					}
				}).catch(err => {
					uni.showToast({
						title: '获取数据失败',
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #F6F7FB;
	}
	
	.header {
		background-color: #FFAC3E;
		width: 100%;
		height: 250rpx;
		border-radius: 0rpx 0rpx 20rpx 20rpx;
		position: relative;
		
		.title {
			color: #fff;
			padding: 30rpx;
			font-size: 40rpx;
			font-weight: bold;
		}
	}
	
	.tabs {
		display: flex;
		background-color: #fff;
		margin: 20rpx;
		border-radius: 12rpx;
		
		.tab-item {
			flex: 1;
			text-align: center;
			padding: 20rpx 0;
			font-size: 28rpx;
			color: #666;
			position: relative;
			
			&.active {
				color: #FFAC3E;
				font-weight: bold;
				
				&::after {
					content: '';
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
					width: 40rpx;
					height: 4rpx;
					background-color: #FFAC3E;
					border-radius: 2rpx;
				}
			}
		}
	}
	
	.content {
		margin: 20rpx;
	}
	
	.scroll-view {
		height: calc(100vh - 250rpx - 40rpx);
	}
	
	.rank-list {
		background-color: #fff;
		border-radius: 12rpx;
		overflow: hidden;
		
		.table-scroll {
			width: 100%;
			white-space: nowrap;
		}
		
		.table-content {
			display: inline-block;
			min-width: 100%;
		}
		
		.rank-header {
			display: flex;
			background-color: #F6F7FB;
			padding: 20rpx 0;
			font-size: 24rpx;
			color: #666;
			border-bottom: 1rpx solid #eee;
		}
		
		.rank-item {
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #eee;
			
			&:last-child {
				border-bottom: none;
			}
		}
		
		.rank-col {
			text-align: center;
			font-size: 24rpx;
			flex-shrink: 0;
			
			&.rank {
				width: 80rpx;
			}
			
			&.user-info {
				width: 200rpx;
				display: flex;
				align-items: center;
				justify-content: flex-start;
				padding-left: 20rpx;
				
				.avatar {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					margin-right: 10rpx;
				}
				
				.nickname {
					max-width: 120rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
			
			&.amount {
				width: 120rpx;
				color: #FFAC3E;
				font-weight: bold;
			}
			
			&.count {
				width: 100rpx;
			}
		}
	}
</style> 